import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:oi_ai_tool/common/IconFontIcons.dart';
import 'package:oi_ai_tool/controller/home_page_controller.dart';
import 'package:oi_ai_tool/route/oi_route.dart';
import 'package:oi_ai_tool/util/draw_util.dart';
import 'package:oi_ai_tool/view/home/home_body.dart';
import 'package:oi_ai_tool/view/archive/oi_archive.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, this.title = ''});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final HomePageController homePageController = Get.find<HomePageController>();

  // int _counter = 0;

  navBarItem(IconData iconData) {
    return BottomNavigationBarItem(icon: Icon(iconData), label: "");
  }

  @override
  Widget build(BuildContext context) {
    double mainHeight = ScreenUtil().screenHeight -
        ScreenUtil().bottomBarHeight -
        ScreenUtil().statusBarHeight;
    homePageController.mainHeight = mainHeight;
    homePageController.viewinsetsBootm =
        MediaQuery.of(context).viewInsets.bottom;
    homePageController.initFloatButton();
    return GetBuilder<HomePageController>(builder: (buildercontext) {
      return Scaffold(
        // resizeToAvoidBottomInset: false,
        appBar: AppBar(
          leading: Builder(
            builder: (context) => CupertinoButton(
                onPressed: () => onArchive(context),
                child: const Icon(IconFontIcons.iconMore)),
          ),
          title: Text(widget.title),
          actions: [
            CupertinoButton(onPressed: onSetting, child: const Icon(Icons.settings))
          ],
        ),
        body: const HomeBody(),
        floatingActionButton: Visibility(
          visible: homePageController.floatButtonVisible,
          child: Container(
            margin: EdgeInsets.only(bottom: 55.sp),
            child: FloatingActionButton(
                onPressed: homePageController.archive,
                child: const Icon(
                  Icons.edit_note,
                )),
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
        // bottomNavigationBar: BottomNavigationBar(
        //   currentIndex: homePageController.tabIndex,
        //   items: [
        //     navBarItem(Icons.home),
        // navBarItem(Icons.add),
        // navBarItem(Icons.more_horiz_sharp)
        //   ],
        //   onTap: homePageController.onSelectTab,
        // ),
        drawer: Drawer(
          child: OiArchive(
            pcontext: context,
          ),
        ),
      );
    });
  }

  void onArchive(context) {
    DrawUtil(context).open();
    // Scaffold.of(context).openDrawer();
    // OiRoute.go(Routes.Mine);
  }

  void onSetting() {
    OiRoute.off(
      Routes.Setting,
    );
  }

  @override
  void dispose() {
    super.dispose();
    homePageController.dispose();
  }
}
